// File Name: key_logic.c
// Author: kukukukuku~
// Date: 2024-06-25

#include "./User/Beep/inc/beep_logic.h"
#include "./User/Key/inc/key_logic.h"
#include "./User/OLED/inc/oled_task.h"
#include "./User/Step_Motor/inc/step_motor_ctrl.h"
#include "FreeRTOS.h"
#include "task.h"
#include "stdio.h"

/**
 * @brief   按键扫描
 * @param   keyId:  按键对象
 * @retval  None
 * @note    None
*/
void key_scan(pKey keyId)
{
    if(keyId == NULL)
    {
        return;
    }
    
    //检测到按键按下
    if(keyId->pressFlag == 0 && gpio_input_data_bit_read(keyId->gpioPort, keyId->gpioPin) == keyId->pressPolarity)
    {
        vTaskDelay(15);
        //延时消抖后还是检测到按键按下
        if(gpio_input_data_bit_read(keyId->gpioPort, keyId->gpioPin) == keyId->pressPolarity)
        {
            keyId->pressFlag = 1;
            keyId->pressCount++;
            
            //如果前一状态所有按键都松开且按键长按判断定时器停止，则启动按键长按判断定时器
            if(keyAllReleaseFlag && KEY_TMR->ctrl1_bit.tmren != TRUE)
            {
                keyAllReleaseFlag = 0;
                tmr_counter_value_set(KEY_TMR, 0);
                tmr_counter_enable(KEY_TMR, TRUE);
            }
            
            keyId->state = KEY_STATE_CLICK;
            keyId->longPressJudgeCnt = 0;
            keyId->longPressScanFlag = 1;
        }
    }
    //检测到按键松开
    else if(gpio_input_data_bit_read(keyId->gpioPort, keyId->gpioPin) != keyId->pressPolarity)
    {
        keyId->pressFlag = 0;
        keyId->longPressJudgeCnt = 0;
        keyId->longPressScanFlag = 0;
        
        //单击后松开，执行单击处理
        if(keyId->state == KEY_STATE_CLICK && keyId->handle == KEY_HANDLE_NONE)
        {
            keyId->handle = KEY_HANDLE_CLICK;
            beep_alarm(keyClickBeep);
        }
        //长按后松开，清除处理标志
        else if(keyId->state == KEY_STATE_CLICK_LONG)
        {
            keyId->handle = KEY_HANDLE_NONE;
        }
        
        keyId->state = KEY_STATE_RELEASE;
    }
}


/**
 * @brief   UP按键处理
 * @param   None
 * @retval  None
 * @note    None
*/
void key_up_handle(void)
{
    if(key[KEY_UP_INDEX].handle == KEY_HANDLE_LONG)
    {
        key[KEY_UP_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
    }
    else if(key[KEY_UP_INDEX].handle == KEY_HANDLE_CLICK)
    {
        key[KEY_UP_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED设置界面, UP按键用做选项上移
        else if(oledDisModeFlag && oledItemUpDnEnaFlag)
        {
            oledItemUpFlag = 1;
        }
    }
}


/**
 * @brief   LEFT按键处理
 * @param   None
 * @retval  None
 * @note    None
*/
void key_left_handle(void)
{
    if(key[KEY_LEFT_INDEX].handle == KEY_HANDLE_LONG)
    {
        key[KEY_LEFT_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED显示主界面时，长按LEFT键Step_Motor反转
        else if(oledDisModeFlag == 0)
        {
            stepMotorState = STEP_MOTOR_CCW;
        }
    }
    else if(key[KEY_LEFT_INDEX].handle == KEY_HANDLE_CLICK)
    {
        key[KEY_LEFT_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED设置界面, LEFT按键用做数值减小
        else if(oledMenuLevel && oledValSetEnaFlag)
        {
            oledValSetSubFlag = 1;
        }
    }
}


/**
 * @brief   DOWN按键处理
 * @param   None
 * @retval  None
 * @note    None
*/
void key_down_handle(void)
{
    if(key[KEY_DOWN_INDEX].handle == KEY_HANDLE_LONG)
    {
        key[KEY_DOWN_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
    }
    else if(key[KEY_DOWN_INDEX].handle == KEY_HANDLE_CLICK)
    {
        key[KEY_DOWN_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED设置界面, DOWN按键用做选项下移
        else if(oledDisModeFlag && oledItemUpDnEnaFlag)
        {
            oledItemDnFlag = 1;
        }
    }
}


/**
 * @brief   RIGHT按键处理
 * @param   None
 * @retval  None
 * @note    None
*/
void key_right_handle(void)
{
    if(key[KEY_RIGHT_INDEX].handle == KEY_HANDLE_LONG)
    {
        key[KEY_RIGHT_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED显示主界面时，长按RIGHT键Step_Motor正转
        else if(oledDisModeFlag == 0)
        {
            stepMotorState = STEP_MOTOR_CW;
        }
    }
    else if(key[KEY_RIGHT_INDEX].handle == KEY_HANDLE_CLICK)
    {
        key[KEY_RIGHT_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED设置界面, RIGHT按键用做数值增大
        else if(oledMenuLevel && oledValSetEnaFlag)
        {
            oledValSetPlusFlag = 1;
        }
    }
}


/**
 * @brief   MID按键处理
 * @param   None
 * @retval  None
 * @note    None
*/
void key_mid_handle(void)
{
    if(key[KEY_MID_INDEX].handle == KEY_HANDLE_LONG)
    {
        key[KEY_MID_INDEX].handle = KEY_HANDLE_NONE;
        
        //唤醒屏幕
        if(oledOffFlag)
        {
            oledOffFlag = 0;
            oledWarnOledAwakenedFlag = 1;
            oledWarnKeyLockedFlag = 0;
            oledWarnKeyUnlockedFlag = 0;
            oled_display_on();
            vTaskResume(taskHandleOled);
        }
        //如果按键锁定，解锁，OLED显示警告
        else if(keyLockFlag)
        {
            keyLockFlag = 0;
            oledWarnKeyUnlockedFlag = 1;
        }
        else if(oledDisModeFlag == 0)
        {
            oledDisModeFlag = 1;
        }
        else if(oledDisModeFlag && oledValSetEnaFlag)
        {
            oledItemSelectFlag = 2;
        }
    }
    else if(key[KEY_MID_INDEX].handle == KEY_HANDLE_CLICK)
    {
        key[KEY_MID_INDEX].handle = KEY_HANDLE_NONE;
        
        //如果按键锁定，OLED显示警告
        if(keyLockFlag)
        {
            oledWarnKeyLockedFlag = 1;
        }
        //OLED设置界面, MID按键用做选项确认
        else if(oledDisModeFlag && (oledItemUpDnEnaFlag || oledValSetEnaFlag))
        {
            oledItemSelectFlag = 1;
        }
    }
}
